# -*- coding: utf-8 -*-

import json
import datetime
import decimal
from bson import ObjectId

'''
    把数据库查询到的数据转换为json格式的工具类
'''


class MyJsonEncoder(json.JSONEncoder):
    '''
        重写构造json类，对日期等格式进行特殊处理
    '''

    def default(self, obj):
        if isinstance(obj, datetime.datetime):
            return obj.strftime('%Y-%m-%d %H:%M:%S')
        elif isinstance(obj, datetime.date):
            return obj.strftime('%Y-%m-%d')
        elif isinstance(obj, decimal.Decimal):
            return str(obj)
        elif isinstance(obj, ObjectId):
            return str(obj)
        else:
            return json.JSONEncoder.default(self, obj)


class Table2Json(object):
    '''
        工具类，把MySQL数据库获取的数据转换为JSON格式
    '''

    @staticmethod
    def view_area_list_to_json(lists):
        """
        省市县列表json转换
        :param lists:
        :return:
        """
        data = []
        for row in lists:
            result = {}
            result['id'] = row[0]
            result['name'] = row[1]

            data.append(result)

        # 使用json.dumps将数据转换为json格式，json.dumps方法默认会输出成这种格式"\u5377\u76ae\u6298\u6263"，
        # 加ensure_ascii=False，则能够防止中文乱码。
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)

        return jsonData

    @staticmethod
    def view_dept_list_to_json(lists):
        data = []
        for row in lists:
            result = {}
            result['deptId'] = row[0]
            result['deptName'] = row[1]
            result['deptPcode'] = row[2]
            result['deptCode'] = row[3]
            result['deptIsLock'] = row[4]
            result['deptSort'] = row[5]
            result['deptAddress'] = row[6]
            result['leveCode'] = row[7]
            result['provinceId'] = row[8]
            result['cityId'] = row[9]
            result['districtId'] = row[10]
            result['deptUrl'] = row[11]
            result['desUrl'] = row[12]
            result['extractFrom'] = row[13]
            result['levelName'] = row[14]
            result['provinceName'] = row[15]
            result['cityName'] = row[16]
            result['districtName'] = row[17]

            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)

        return jsonData

    @staticmethod
    def view_favor_cart_item_list_to_json(lists):
        data = []
        for row in lists:
            result = {}
            result['id'] = row[0]
            result['userId'] = row[1]
            result['isLock'] = row[2]
            result['itemId'] = row[3]
            result['deptId'] = row[4]
            result['itemHandlername'] = row[5]
            result['itemHandlerphone'] = row[6]
            result['itemTitle'] = row[7]
            result['itemPricerange'] = row[8]
            result['itemContent'] = row[9]
            result['itemUrl'] = row[10]
            result['itemDeadline'] = row[11]
            result['itemPublishdate'] = row[12]
            result['itemAddress'] = row[13]
            result['itemStatus'] = row[14]

            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)

        return jsonData

    @staticmethod
    def admin_list_to_json(lists):
        """
        把从admin表查询出的信息转换为json格式，增加返回roleName字段（17-5-25）
        :param lists:
        :return:
        """
        data = []
        for row in lists:
            result = {}
            result['adminId'] = row[0]
            result['roleId'] = row[1]
            result['roleType'] = row[2]
            result['adminName'] = row[3]
            result['adminPassword'] = row[4]
            result['adminSalt'] = row[5]
            result['adminDesc'] = row[6]
            result['adminRealName'] = row[7]
            result['adminTelephone'] = row[8]
            result['adminEmail'] = row[9]
            result['adminIsLock'] = row[10]
            result['adminAddTime'] = row[11]
            result['roleName'] = row[12]

            data.append(result)

        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)

        return jsonData

    @staticmethod
    def view_item_list_to_json(lists):
        """
        把从 view_item 表查询出的信息转换为json格式
        :param lists:
        :return:
        """
        data = []
        for row in lists:
            result = {}
            result['itemId'] = row[0]
            result['itemHandlerName'] = row[1]
            result['itemHandlerPhone'] = row[2]
            result['itemTitle'] = row[3]
            result['itemPriceRange'] = row[4]
            result['itemContent'] = row[5]
            result['itemUrl'] = row[6]
            result['itemDeadline'] = row[7]
            result['itemPublishdate'] = row[8]

            result['itemAddress'] = row[9]
            result['itemStatus'] = row[10]

            result['serviceId'] = row[11]
            result['serviceName'] = row[12]
            result['serviceCode'] = row[13]
            result['servicePrice'] = row[14]

            result['serviceRate'] = row[15]
            result['serviceStatus'] = row[16]
            result['serviceContent'] = row[17]
            result['serviceContract'] = row[18]
            result['serviceStartDate'] = row[19]

            result['serviceEndDate'] = row[20]
            result['deptId'] = row[21]
            result['deptName'] = row[22]
            result['deptPcode'] = row[23]
            result['deptCode'] = row[24]
            result['deptIsLock'] = row[25]

            result['deptSort'] = row[26]
            result['deptAddress'] = row[27]
            result['leveCode'] = row[28]
            result['provinceId'] = row[29]
            result['cityId'] = row[30]
            result['districtId'] = row[31]

            result['itemEnterPerson'] = row[32]
            result['itemEnterTime'] = row[33]
            result['checkStatusOne'] = row[34]
            result['checkRemarkOne'] = row[35]
            result['checkPersonOne'] = row[36]
            result['checkTimeOne'] = row[37]
            result['checkStatusTwo'] = row[38]
            result['checkRemarkTwo'] = row[39]
            result['checkPersonTwo'] = row[40]
            result['checkTimeTwo'] = row[41]

            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)
        return jsonData

    @staticmethod
    def view_all_items_to_json(lists):
        """
        把从 view_item 表查询出的信息转换为json格式
        :param lists:
        :return:
        """
        data = []
        for row in lists:
            result = {}
            result['itemId'] = row[0]
            result['deptId'] = row[1]
            result['itemHandlername'] = row[2]
            result['itemHandlerphone'] = row[3]
            result['itemTitle'] = row[4]
            result['itemPricerange'] = row[5]
            result['itemContent'] = row[6]
            result['itemUrl'] = row[7]
            result['itemDeadline'] = row[8]
            result['itemPublishdate'] = row[9]
            result['itemAddress'] = row[10]
            result['itemStatus'] = row[11]
            result['serviceId'] = row[12]
            result['itemEnterPerson'] = row[13]
            result['itemEnterTime'] = row[14]
            result['checkStatusOne'] = row[15]
            result['checkRemarkOne'] = row[16]
            result['checkPersonOne'] = row[17]
            result['checkTimeOne'] = row[18]
            result['checkStatusTwo'] = row[19]
            result['checkRemarkTwo'] = row[20]
            result['checkPersonTwo'] = row[21]
            result['checkTimeTwo'] = row[22]
            result['id'] = row[23]
            result['deptName'] = row[24]
            result['deptPcode'] = row[25]
            result['deptCode'] = row[26]
            result['deptSort'] = row[27]
            result['deptAddress'] = row[28]
            result['leveCode'] = row[29]
            result['provinceId'] = row[30]
            result['cityId'] = row[31]
            result['districtId'] = row[32]
            result['deptUrl'] = row[33]
            result['desUrl'] = row[34]
            result['levelName'] = row[35]
            result['provinceName'] = row[36]
            result['cityName'] = row[37]
            result['districtName'] = row[38]
            result['catId'] = row[39]
            result['catName'] = row[40]
            result['catPcode'] = row[41]
            result['catCode'] =row[42]
            result['catStatus'] = row[43]
            result['catSort'] = row[44]
            result['deptIsLock'] = row[45]
            result['industryId'] = row[46]
            result['industryName'] = row[47]
            result['industryPcode'] = row[48]
            result['industryCode'] = row[49]
            result['industryIsLock'] = row[50]
            result['industrySort'] = row[51]

            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)
        return jsonData


    @staticmethod
    def view_dept_category_list_to_json(lists):
        """
        把从 view_dept_category 表查询出的信息转换为json格式
        :param lists:
        :return:
        """
        data = []
        for row in lists:
            result = {}
            result['id'] = row[0]
            result['catId'] = row[1]
            result['catName'] = row[2]
            result['catPcode'] = row[3]
            result['catCode'] = row[4]
            result['catStatus'] = row[5]
            result['catSort'] = row[6]
            result['deptId'] = row[7]
            result['deptName'] = row[8]

            result['deptPcode'] = row[9]
            result['deptCode'] = row[10]

            result['deptIsLock'] = row[11]
            result['deptSort'] = row[12]
            result['deptAddress'] = row[13]
            result['leveCode'] = row[14]

            result['provinceId'] = row[15]
            result['cityId'] = row[16]
            result['districtId'] = row[17]
            result['deptUrl'] = row[18]
            result['desUrl'] = row[19]

            result['extractFrom'] = row[20]
            result['extractDate'] = row[21]

            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)
        return jsonData

    @staticmethod
    def view_indus_item_list_to_json(lists):
        """
        把从 view_dept_category 表查询出的信息转换为json格式
        :param lists:
        :return:
        """
        data = []
        for row in lists:
            result = {}
            result['id'] = row[0]
            result['industryId'] = row[1]
            result['industryName'] = row[2]
            result['industryPcode'] = row[3]
            result['industryCode'] = row[4]
            result['industryIsLock'] = row[5]
            result['industrySort'] = row[6]
            result['itemId'] = row[7]
            result['deptId'] = row[8]

            result['itemHandlername'] = row[9]
            result['itemHandlerphone'] = row[10]

            result['itemTitle'] = row[11]
            result['itemPricerange'] = row[12]
            result['itemContent'] = row[13]
            result['itemUrl'] = row[14]

            result['itemDeadline'] = row[15]
            result['itemPublishdate'] = row[16]
            result['itemAddress'] = row[17]
            result['itemStatus'] = row[18]
            result['serviceId'] = row[19]

            result['itemEnterPerson'] = row[20]
            result['itemEnterTime'] = row[21]

            result['checkStatusOne'] = row[22]
            result['checkRemarkOne'] = row[23]
            result['checkPersonOne'] = row[24]

            result['checkTimeOne'] = row[25]
            result['checkStatusTwo'] = row[26]
            result['checkRemarkTwo'] = row[27]
            result['checkPersonTwo'] = row[28]
            result['checkTimeTwo'] = row[29]

            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)
        return jsonData
